import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
// 加载 Vant 核心组件库
import vant from "vant";
import { Toast } from "vant";
// 加载 Vant 全局样式
import "vant/lib/index.css";
import "amfe-flexible";
import components from "@/components";
import { Lazyload } from "vant";
// 导入全局的样式文件
import "./styles/index.scss";
import * as directives from "@/directives";
const whitelist = ["/my", "/my/identCheck/login"];
router.beforeEach((to, from, next) => {
  if (!store.state.user) {
    if (whitelist.indexOf(to.path) > -1) {
      next();
    } else {
      try {
        next("/my");
        Toast.fail({
          message: "只能登录后进行跳转",
          icon: "clear"
        });
      } catch (error) {
        return Promise.reject(error);
      }
    }
  } else {
    next();
  }
});
Vue.use(vant);
Vue.use(Lazyload);
// 全局遍历注册自定义指令
Object.keys(directives).forEach(key => {
  Vue.directive(key, directives[key]);
});
Vue.use(components);
Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
